home *** CD-ROM | disk | FTP | other *** search
/ CD-ROM Data 2002 May / CD Rom Data Mayıs 2002.iso / Freeware / Blitz Basic / data1.cab / Support / help / samples / mze.bb < prev    next >
Encoding:
Text File  |  2002-04-10  |  15.5 KB  |  349 lines

  1. ;--------------------------------------------------------------------------
  2. ;                                                                rno 130800
  3. ;                                                         noospot42@f2s.com
  4. ; Mandelbrot set Zooming Explorer
  5. ;
  6. ;images are saved in current directory with name including fractal type, center coordinates 
  7. ;and frame number, using ZeroMe() function by Mikkel Lokke <flameduck@software.dk>
  8. ;
  9. ;original algorythm by Mandelbrot :)
  10. ;
  11. ; press ESC anytime to quit
  12. ; verified 1.48 4/18/2001
  13. ;--------------------------------------------------------------------------
  14.  AppTitle(" - mze - rno 2000 - ")
  15.  
  16.    Const disp_width# = 640                               ;display width
  17.   Const disp_height# = 480                                ;display height
  18.     Const dispx_ctr# = disp_width/2                       ;center x
  19.     Const dispy_ctr# = disp_height/2                      ;       y
  20.  
  21. ;change pixel size if it's too fast / too slow on your system  (absolute value ok):
  22.   Const def_pix_siz# = disp_width/64                      ;default size of a pixel
  23.  
  24. ; same but for the high quality pic used to choose where to zoom to:
  25.   Const hiq_pix_siz# = 1                                  ;high quality mode pixel size
  26.      Global pix_siz# = def_pix_siz                        ;size of a pixel
  27.  
  28. ;256 is close to the highest value you can use as number of frames in a zoom with this zooming factor (.96)
  29. ;because of float number resolution, after a while rounded values errors propagate too much
  30. ;if you stop zooming when close to last frame (256th) you'll see how the picture is trashed
  31.         Const fr_max = 256                                ;number of frames in a zoom
  32.        Global fr_cur = 0                                  ;number of frames counter
  33.     Global fr_width# = disp_width/pix_siz                 ;width of frame in 'fzpixels'
  34.    Global fr_height# = disp_height/pix_siz
  35.      Global fr_state = 0                                  ;is frame finished rendering(1) or not (0)
  36.      Global fr_saved = 0                                  ;if frame saved already (1) or not (0)
  37. Global fr_grab_name$ 
  38.  
  39.    Const def_fx_rng# = 6.4                                ;default starting boundaries range
  40.    Const def_fy_rng# = def_fx_rng*(disp_height/disp_width)
  41.    Const def_fx_ctr# = 0                                  ;default starting center
  42.    Const def_fy_ctr# = 0
  43.  
  44.       Global fx_rng# = def_fx_rng                         ;boundaries range
  45.       Global fy_rng# = def_fy_rng 
  46.       Global fx_ctr# = def_fx_ctr                         ;center x
  47.       Global fy_ctr# = def_fy_ctr                         ;       y
  48.       Global fx_stp# = fx_rng/fr_width                    ;step to next x
  49.       Global fy_stp# = fy_rng/fr_height                   ;             y
  50.       Global fx_min# = fx_ctr-(fx_rng/2)                  ;first x
  51.       Global fy_min# = fy_ctr-(fy_rng/2)                  ;      y
  52.       Global fx_max# = fx_ctr+(fx_rng/2)                  ;last x
  53.       Global fy_max# = fy_ctr+(fy_rng/2)                  ;     y
  54.  
  55.       Global fx_cur# = fx_min                             ;current (fractal) x and y
  56.       Global fy_cur# = fy_min
  57.  
  58.       Global fx_fac# = disp_width/fx_rng                  ;diplaying factor
  59.       Global fy_fac# = disp_height/fy_rng 
  60.  
  61.       Global fx_ori# = dispx_ctr-(fx_ctr*fx_fac)          ;point to set origin to
  62.       Global fy_ori# = dispy_ctr-(fy_ctr*fy_fac)
  63.  
  64.       Const def_thr# = 4                                  ;default threshold for mandelbrot set calc
  65.  Const def_iter_max# = 127                                ;default iterations maximum for mandelbrot set
  66.  
  67.      Global fz_state = 0                                  ;program state
  68. Global fz_prev_state = 0                                  ;previous program state
  69.       Const calc = 0                                      ; state can be one of:
  70.       Const done = 1
  71.       Const zoom = 2
  72.     Const unzoom = 3
  73.        Const hiq = 4
  74.       Const menu = 5
  75.  
  76.       Global fz_frac = 0                                  ;type of fractal rendered
  77.    Global fz_frac_tn = 2                                  ;number of different types
  78. Const mandelbrot = 0 
  79.    Const tristar = 1
  80. ;    Const dragon = 2
  81.  
  82.            Const zf# = .96                                ;zooming factor
  83.  
  84. Global mzx#,mzy#                                          ;mouse values 
  85. Graphics disp_width,disp_height                     ;,32  if your card support 32 bits it's faster 
  86. Global fr_grab=CreateImage(disp_width,disp_height)        ;grabbed image
  87.  
  88. ;--------------------------------------------------------------------------
  89. ;main loop
  90. ;
  91.  
  92. SetBuffer BackBuffer()                                              ;use doublebuffer
  93. Cls
  94. Color 155,255,202                                         ;text color
  95. Text 0,0,  "mandelbrot zooming explorer - rno 2000"
  96. Text 0,16,"   ESC to quit anytime"
  97. Text 0,32,"During zoom/unzoom"
  98. Text 0,48,"   Spacebar to stop zoom"
  99. Text 0,64,"While stopped (once pic finnished)"
  100. Text 0,80,"   RMB : zoom   LMB : unzoom   S : save pic  T : change fractal"
  101. Text 0,96,"   click then go a few steps away from your monitor :) "
  102.  
  103. Text 0,112,"press a key....."
  104. Flip                                                      ;show buffer
  105. WaitKey() 
  106.                                           
  107. SetBuffer FrontBuffer()                                   ;use front buffer because pic is gonna be hi q
  108. Cls
  109. Origin fx_ori,fy_ori                 ;sets drawing origin
  110. fz_state=hiq
  111. precision(hiq_pix_siz)
  112. fz_prev_state=zoom
  113. fr_state=calc
  114.  
  115. While Not KeyDown(1)                                      ;it will never stop (if both you and Windows agree)
  116.  If fr_state=calc                                         ;frame in process
  117.     frame_mandel()
  118.   Else
  119.  
  120.     Select fz_state                                       ;frame done select what's next 
  121.       Case zoom
  122.         Flip
  123.         fr_cur=fr_cur+1
  124.         upd_values(fx_rng*zf,fy_rng*zf,mzx,mzy)
  125.         fr_state=calc
  126.         If fr_cur=>fr_max
  127.            fz_state=unzoom
  128.         EndIf
  129.       Case unzoom
  130.         Flip
  131.         fr_cur=fr_cur-1
  132.         upd_values(fx_rng/zf,fy_rng/zf,mzx,mzy)
  133.         fr_state=calc
  134.         If fr_cur<=0
  135.            fz_state=zoom
  136.         EndIf
  137.       Case hiq
  138.         GrabImage(fr_grab,fx_min*fx_fac,fy_min*fy_fac)
  139.         SetBuffer BackBuffer()
  140. Origin fx_ori,fy_ori                 ;sets drawing origin
  141.         fr_saved=0
  142.         Repeat 
  143.           mouse()
  144.           If KeyDown(1) Exit                            
  145.           If MouseDown(1) Then fz_state=zoom:precision(def_pix_siz):Exit
  146.           If MouseDown(2) Then fz_state=unzoom:precision(def_pix_siz):Exit
  147.           If KeyDown(20)                                        ;if T key pressed         
  148.              fz_frac=(fz_frac+1)Mod fz_frac_tn                  ;toggle fractal type
  149.              fr_state=calc
  150.              fr_grab=CreateImage(disp_width,disp_height)        ;create hiq image 
  151.              SetBuffer FrontBuffer()
  152.              Origin fx_ori,fy_ori                 ;sets drawing origin            
  153.              Exit
  154.           EndIf
  155.           If KeyDown(31) And fr_saved=0                         ;if S key pressed and image not saved yet
  156.              fr_grab_name = "mze"+fz_frac+"_"+fx_ctr+"_"+fy_ctr+"_"+ZeroMe(3,fr_cur)+".bmp"  ;compose a name to save frame with
  157.              SaveImage fr_grab,fr_grab_name                     ;save the frame
  158.              fr_saved=1                                         ;
  159.           EndIf
  160.         Forever
  161.      End Select
  162.  
  163.      If KeyDown(57)                                       ;if spacebar is pressed
  164.         fz_prev_state=fz_state
  165.         fz_state=hiq
  166.         fr_state=calc
  167.         precision(hiq_pix_siz)
  168.  
  169.         FreeImage fr_grab
  170.         fr_grab=CreateImage(disp_width,disp_height)       ;create hiq image
  171.         SetBuffer FrontBuffer()
  172. Origin fx_ori,fy_ori                 ;sets drawing origin
  173.      EndIf
  174.  
  175.  EndIf
  176.  
  177. Wend
  178. ;--------------------------------------------------------------------------
  179. FreeImage fr_grab
  180. EndGraphics
  181. End
  182.  
  183. ;--------------------------------------------------------------------------
  184. ; upd_values(x boundaries range#, y bnd range#, x center#, y center#)
  185. ; when one of the key values for the frame is changed, used to update the whole set accordingly
  186. ;
  187. Function upd_values(new_x_rng#,new_y_rng#,new_fx_tctr#,new_fy_tctr#)
  188.  fx_rng = new_x_rng                                       ;boundaries range x
  189.  fy_rng = new_y_rng                                       ;                 y
  190.  fx_ctr = new_fx_tctr                                     ;center x
  191.  fy_ctr = new_fy_tctr
  192.  fx_stp = fx_rng/fr_width                                 ;step to next x
  193.  fy_stp = fy_rng/fr_height                                ;             y
  194.  fx_min = fx_ctr-(fx_rng/2)                               ;first x
  195.  fy_min = fy_ctr-(fy_rng/2)                               ;      y
  196.  fx_max = fx_ctr+(fx_rng/2)                               ;last x
  197.  fy_max = fy_ctr+(fy_rng/2)                               ;     y
  198.  fx_cur = fx_min                                          ;current x and y = top left
  199.  fy_cur = fy_min
  200.  fx_fac = disp_width/fx_rng                               ;diplaying factor
  201.  fy_fac = disp_height/fy_rng 
  202.  fx_ori = dispx_ctr-(fx_ctr*fx_fac)                       ;point to set origin to
  203.  fy_ori = dispy_ctr-(fy_ctr*fy_fac)
  204.  Origin fx_ori,fy_ori
  205. End Function
  206.  
  207. ;--------------------------------------------------------------------------
  208. ; frame_mandel()
  209. ; draw a point using iter_mandel color and then go to the next point, reset values if no next point 
  210. ;
  211. Function frame_mandel()                                   ;there is no loop first point should be set already
  212.  Select fz_frac
  213.    Case 0
  214.      iter_mandel(fx_cur,fy_cur,def_thr,def_iter_max) 
  215.    Case 2
  216.      iter_dragon_mandel(fx_cur,fy_cur,def_thr,def_iter_max)
  217.    Case 1
  218.      iter_tristar_mandel(fx_cur,fy_cur,def_thr,def_iter_max)  ;so go iterate with that one
  219.  End Select
  220.  Rect fx_cur*fx_fac,fy_cur*fy_fac,pix_siz,pix_siz         ;draw it
  221.  fx_cur=fx_cur+fx_stp                                     ;next column
  222.  If fx_cur=>fx_max                                        ;if last column
  223.     fx_cur=fx_min                                         ;   first column
  224.     fy_cur=fy_cur+fy_stp                                  ;   next row
  225.     If fy_cur>=fy_max                                     ;   if last row
  226.        fy_cur=fy_min                                      ;      first row
  227.        fr_state=done                                      ;      frame is done
  228.     EndIf   
  229.  EndIf
  230. End Function
  231.  
  232. ;--------------------------------------------------------------------------
  233. ; iter_mandel(x#, y#, threshold%, iterations maximum%)
  234. ; changes current color according to number of iterations needed before threshold for current point
  235. ; x and y coords are real coordinates (signed float)
  236. ; the higher iteration maximum, the more complex the pic, but calc time rises quite fast
  237. ;     Const def_thr = 4
  238. ;Const def_iter_max = 96
  239. ;
  240. Function iter_mandel(x_mand#,y_mand#,thr#,mxitr#)         ;max number of iterations 
  241.   Local itr = 0                                           ;reset iteration counter
  242.    Local p# = 0                                           ;reset p and q
  243.    Local q# = 0
  244.   Local op# = 0                                           ;will store p and q previous value
  245.   Local oq# = 0          
  246.   While  op+oq<thr And itr < mxitr                        ;while under threshold or iteration maximum
  247.       q = p*q*2 + y_mand                                  ;it's a kind of magic
  248.       p = op-oq + x_mand                                  ;magic
  249.      op = p*p                                             ;mah-gic
  250.      oq = q*q                                             ;maygeeeeeeeeeeeeeeeek 
  251.     itr = itr+1                                           ;( guitar solo )
  252.   Wend                                                    ;ok sorry so next iteration 
  253.   Color  op Shl 2,oq Shl 2,itr Shl 1                      ;iteration # determine color 
  254. End Function 
  255. ;
  256. ;
  257. Function iter_dragon_mandel(x_mand#,y_mand#,thr#,mxitr#)  ;max number of iterations 
  258.   Local itr = 0                                           ;reset iteration counter
  259.    Local p# = 0.5                                         ;reset p and q
  260.    Local q# = 0
  261.   Local op# = 0.5                                         ;will store p and q previous value
  262.   Local oq# = 0
  263.       While ( (p*p)+(q*q)<thr And itr < mxitr)
  264.         p=(oq-op)*(oq+op) + op
  265.         q=op*oq
  266.         q=q + q - oq
  267.         op=(x_mand * p) + (y_mand * q)
  268.         oq=(y_mand * p) - (x_mand * q)
  269.         itr=itr + 1
  270.       Wend
  271.   Color  0,mxitr-itr,op Shl 2;q Shl 2                      ;iteration # determine color 
  272.  
  273. End Function 
  274. ;
  275. ;
  276. Function iter_tristar_mandel(x_mand#,y_mand#,thr#,mxitr#) ;max number of iterations 
  277.   Local itr = 0                                           ;reset iteration counter
  278.    Local p# = 0                                           ;reset p and q
  279.    Local q# = 0
  280.   Local op# = 0                                           ;will store p and q previous value
  281.   Local oq# = 0
  282.       While ( op+oq<thr And itr < mxitr)
  283.          p = p*q*2 + y_mand
  284.          q = op-oq + x_mand
  285.         op = p*p
  286.         oq = q*q
  287.        itr = itr + 1
  288.       Wend
  289.   Color  op Shl 2,itr Shl 1,itr                               ;iteration # determine color 
  290. End Function 
  291.  
  292. ;--------------------------------------------------------------------------
  293. ; precision (new pixel size%)
  294. ; set new pixel size and update related variables
  295. ;
  296. Function precision(new_pix_siz)                         
  297.   pix_siz = new_pix_siz
  298.  fr_width = disp_width/pix_siz
  299. fr_height = disp_height/pix_siz
  300.    fx_stp = fx_rng/fr_width
  301.    fy_stp = fy_rng/fr_height
  302. End Function
  303.  
  304. ;--------------------------------------------------------------------------
  305. ; mouse()
  306. ; interface to select a point to zoom to, using the image grabed just before looping  
  307. ;
  308. Function mouse()
  309.     Cls                                                   ;clear drawbuffer
  310.     DrawBlock fr_grab,fx_min*fx_fac,fy_min*fy_fac         ;blit the hiq frame grabed before
  311.     Color 155,255,202                                     ;change color for text
  312.     mzx=MouseX()-fx_ori                                   ;get mouse value and aply origin offset
  313.     mzy=MouseY()-fy_ori 
  314.     Rect mzx,mzy-3,1,7                                    ;draw a cross there
  315.     Rect mzx-3,mzy,7,1
  316.     mzx=mzx/fx_fac                                        ;get real coordinates of that point
  317.     mzy=mzy/fy_fac 
  318.     Text fx_min*fx_fac,fy_min*fy_fac,mzx+" , "+mzy        ;prints them on top left of screen
  319.     Text fx_min*fx_fac,(fy_max*fy_fac)-16,"T type | LMB zoom | RMB unzoom | S save | ESC quit"
  320.     Select fz_frac
  321.       Case 0
  322.         Text (fx_max*fx_fac)-100,(fy_max*fy_fac)-16,"mandelbrot set"
  323.       Case 1
  324.         Text (fx_max*fx_fac)-100,(fy_max*fy_fac)-16,"dragon mandelbrot"
  325.       Case 2
  326.         Text (fx_max*fx_fac)-100,(fy_max*fy_fac)-16,"tristar mandelbrot"
  327.     End Select
  328.     Flip                                                   
  329. End Function
  330.  
  331. ;--------------------------------------------------------------------------
  332. ;ZeroMe() function by Mikkel Lokke                    flameduck@software.dk
  333. ;
  334. Function ZeroMe$(zeros,number)
  335.     num$=number
  336.     If Len(num$)=>zeros
  337.         Return number
  338.     Else
  339.         zero$=""
  340.         For i=1 To zeros
  341.             zero$=zero$+"0"
  342.         Next
  343.         zero$=Left$(zero$,zeros-Len(num$))
  344.         zero$=zero$+num$
  345.         Return zero$
  346.     EndIf
  347. End Function
  348. ;--------------------------------------------------------------------------